package pl.nazaweb.jboot;

import java.awt.AWTException;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import pl.nazaweb.jboot.afk.AutoLogCallback;
import pl.nazaweb.jboot.afk.FishingCallback;
import pl.nazaweb.jboot.afk.FoodEaterCallback;
import pl.nazaweb.jboot.afk.ManaEaterCallback;
import pl.nazaweb.jboot.afk.MouseClickCallback;
import pl.nazaweb.jboot.afk.ScreanCaptureWatcher;
import pl.nazaweb.jboot.afk.TimerThread;
import pl.nazaweb.jboot.utils.EscapeKeyListener;
import pl.nazaweb.jboot.walking.MouseListener;

public class Main {

    public static void main(String[] args) throws Exception {

        GlobalScreen.registerNativeHook();
        GlobalScreen.getInstance().addNativeKeyListener(new EscapeKeyListener());

        //edron fishing
        TimerThread timerThread = new TimerThread(
                new MouseClickCallback(new Point(459, 615)), 4000);
        timerThread.start();
        TimerThread timerThread2 = new TimerThread(
                new MouseClickCallback(new Point(500, 618)), 1000);
        timerThread2.start();

        //runManaEaterOnScreanKeyClick(new Point(459, 615), 100 * 2 * 1000);//sd
        //runManaEaterOnScreanKeyClick(new Point(500, 618), 100 * 2 * 1001);//food

        //runMouseListener();

        //runAfkOnLenovoFlashClient();

        //runAfkOnLenovoVirtualMachine();
        //runAfkOnLenovo();

        // runFoodEater(new Point(1215, 490), 120 * 1000);

        //runFishing(fishingSpace, 1, KeyEvent.VK_F3, fishInBackpack);

        //221, 27 
        //width: 742
        //height : 109
        //direction : 1
        //runMouseListener();


        //ScreanCaptureWatcher captureWatcher = new ScreanCaptureWatcher(new Rectangle(18, 144, 126, 127));
        //captureWatcher.start();
        //int array[][] = FileReaderUtils.readAllMap(new File("c:\\mapa_.jbot"));
        //new WalkinListenerThread().start(); //x=526, y=498
        //WayFinder wayFinder = new WayFinder(array);
        //System.out.println("docel : " + array[526][498]);
        //LinkedList<WayDirection> way = 
        //wayFinder.findWay2(new Point(500, 500), new Point(526, 498));
        //System.out.println("liczba krok : " + way.size());
        //pod 597,336 nad 579, 253   prawo 629, 297 lewo  553, 297
//        Thread.sleep(10000);
//        Robot r = new Robot();
//        while (true) {
//            //r.keyPress(KeyEvent.VK_LEFT);
//            //r.keyRelease(KeyEvent.VK_LEFT);
//                        robotMouseClick(r, 597, 253);
//        }
    }

    public static void runAfkOnLenovoFlashClient() throws Exception {
        runManaEaterOnScreanKeyClick(new Point(459, 615), 100 * 2 * 1000);//sd
        runManaEaterOnScreanKeyClick(new Point(500, 618), 100 * 2 * 1001);//food
        runAutoLogWatcher(new Rectangle(1099, 303, 152, 83));
    }

    public static void runAfkOnLenovoVirtualMachine() throws Exception {
        //runMouseListener();//766, 680
        Rectangle fishingSpace = new Rectangle(319, 30, 465, 64);
        Rectangle fishInBackpack = new Rectangle(1115, 456, 28, 28);
        /*ublic static void runFoodEaterWithFishing(Point ctrlPoint, Point foodPoint, long interval, Rectangle fishingSpace, int direction, Point fishingHotKey, Rectangle fishWatchPoint)*/
        runFoodEaterWithFishing(new Point(579, 624), new Point(1213, 468),/*new Point(544,170)*/ 200 * 1000, fishingSpace, 1, new Point(599, 498), fishInBackpack);
        runManaEaterOnScreanKeyClick(new Point(781, 493), 100 * 2 * 1000);
        //runManaEater(KeyEvent.VK_F6, 60 * 2 * 1000);
        runAutoLogWatcher(new Rectangle(1193, 278, 153, 55));
    }

    public static void runAfkOnLenovo() throws Exception {
        Rectangle fishingSpace = new Rectangle(160, 35, 765, 100);
        Rectangle fishInBackpack = new Rectangle(1115, 475, 29, 31);
        runFoodEaterWithFishing(new Point(545, 176), 120 * 1000, fishingSpace, 1, KeyEvent.VK_F3, fishInBackpack);
        runManaEater(KeyEvent.VK_F6, 60 * 3 * 1000);
        runAutoLogWatcher(new Rectangle(1193, 278, 153, 55));
    }

    public static void runManaEaterOnScreanKeyClick(Point point, int interval) throws AWTException {
        TimerThread timerThread = new TimerThread(new MouseClickCallback(point), interval);
        timerThread.start();
    }

    public static void runFishing(Rectangle fishingSpace, int direction, int fishingHotKey, Rectangle fishWatchPoint) throws AWTException {
        ScreanCaptureWatcher captureWatcher = new ScreanCaptureWatcher(fishWatchPoint,
                new FishingCallback(fishingSpace, direction, fishingHotKey));
        captureWatcher.setInterval(10000);
        captureWatcher.start();
    }

    public static void runFoodEater(Point foodPoint, long interval) throws AWTException {
        TimerThread timerThread = new TimerThread(new FoodEaterCallback(foodPoint), interval);
        timerThread.start();
    }

    public static void runFoodEaterWithFishing(Point ctrlPoint, Point foodPoint, long interval,
            Rectangle fishingSpace, int direction, Point fishingHotKey, Rectangle fishWatchPoint) throws AWTException {
        FoodEaterCallback foodEaterCallback = new FoodEaterCallback(foodPoint, ctrlPoint);
        //foodEaterCallback.setFishingCallback(new FishingCallback(fishingSpace, direction, fishingHotKey));
        TimerThread timerThread = new TimerThread(foodEaterCallback, interval);
        timerThread.start();
    }

    public static void runFoodEaterWithFishing(Point foodPoint, long interval,
            Rectangle fishingSpace, int direction, int fishingHotKey, Rectangle fishWatchPoint) throws AWTException {
        FoodEaterCallback foodEaterCallback = new FoodEaterCallback(foodPoint);
        foodEaterCallback.setFishingCallback(new FishingCallback(fishingSpace, direction, fishingHotKey));
        TimerThread timerThread = new TimerThread(foodEaterCallback, interval);
        timerThread.start();
    }

    public static void runManaEater(int hotKey, long interval) throws AWTException {
        TimerThread timerThread = new TimerThread(new ManaEaterCallback(hotKey), interval);
        timerThread.start();
    }

    public static void runAutoLogWatcher(Rectangle rectangle) throws AWTException {
        ScreanCaptureWatcher logoutWatcher =
                //lenovo              new ScreanCaptureWatcher(new Rectangle(1107, 281, 154, 82),
                new ScreanCaptureWatcher(rectangle, //vm lenovo
                new AutoLogCallback());
        logoutWatcher.setInterval(100);
        logoutWatcher.start();
    }

    public static void runMouseListener() {
        try { //pod 597,336 nad 579, 253   prawo 629, 297 lewo  553, 297
            GlobalScreen.registerNativeHook();
        } catch (NativeHookException ex) {
            System.err.println("There was a problem registering the native hook.");
            System.err.println(ex.getMessage());
            System.exit(1);
        }
        //Construct the example object.
        MouseListener example = new MouseListener();
        //Add the appropriate listeners for the example object.
        GlobalScreen.getInstance().addNativeMouseListener(example);
        GlobalScreen.getInstance().addNativeMouseMotionListener(example);
    }
}
